﻿/*******************************************************
 * 说明：系统类基本扩展
 * 
*******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using FL.DBModule;

namespace System
{
    public static class SystemExtension
    {

        /// <summary>
        /// 获取指定属性的值
        /// 
        /// 使用方法：
        /// var value = obj.GetStepPropertyValue("Property1.Property2.Property3");
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName">
        /// 级联属性过滤串,格式如:属性.子属性.子子属性...
        /// </param>
        /// <returns></returns>
        public static object GetPropertyValue(this BaseEntity obj, string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");

            //如果是层级属性
            if (propertyName.Contains('.'))
            {
                return GetStepPropertyValue(obj, propertyName);
            }

            //object value = null;

            //if (obj.TryGetPropertyValue(propertyName, out value))
            // {
            //    return value;
            //}

            var property = obj.GetType().GetProperty(propertyName);
            Debug.Assert(null != property, "类型" + obj.GetType().ToString() + "不存在属性" + propertyName);
            return property.GetValue(obj, null);
        }

        /// <summary>
        /// 获取指定属性的值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static T GetPropertyValue<T>(this object obj, string propertyName)
        {
            return (T)obj.GetPropertyValue(propertyName);
        }

        /// <summary>
        /// 获取指定属性的值
        /// 
        /// 使用方法：
        /// var value = obj.GetStepPropertyValue("Property1.Property2.Property3");
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName">
        /// 级联属性过滤串,格式如:属性.子属性.子子属性...
        /// </param>
        /// <returns></returns>
        public static object GetPropertyValue(this object obj, string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");

            //如果是层级属性
            if (propertyName.Contains('.'))
            {
                return GetStepPropertyValue(obj, propertyName);
            }

            var property = obj.GetType().GetProperty(propertyName);
            Debug.Assert(null != property, "类型" + obj.GetType().ToString() + "不存在属性" + propertyName);
            return property.GetValue(obj, null);
        }

        /// <summary>
        /// 设置指定属性的值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue(this object obj, string propertyName, object value)
        {
            //if(obj is 
            PropertyInfo p = obj.GetType().GetProperty(propertyName);
            Debug.Assert(null != p, "类型" + obj.GetType().ToString() + "不存在属性" + propertyName);
            p.SetValue(obj, value, null);
        }



        public static bool EqualsIgnorecase(this string obj, string target)
        {
            if (obj == null) throw new ArgumentNullException("obj");

            return string.Compare(obj, target, true) == 0;
        }

        private static object GetStepPropertyValue(object obj, string propertyName)
        {
            var propertiesArray = propertyName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            object value = obj;
            for (int i = 0, c = propertiesArray.Length; i < c; i++)
            {
                if (value == null) throw new ArgumentNullException("value");

                var property = propertiesArray[i];
                value = value.GetPropertyValue(property);
            }

            return value;
        }
    }
}